package com.dongdongshop.controller;

import cn.hutool.json.JSONUtil;
import com.dongdongshop.data.Result;
import com.dongdongshop.enums.ResultEnum;
import com.dongdongshop.page.PageResult;
import com.dongdongshop.pojo.*;
import com.dongdongshop.service.BrandService;
import com.dongdongshop.service.CatService;
import com.dongdongshop.service.GoodsService;
import com.dongdongshop.service.TypeTemplateService;
import com.dongdongshop.vo.TypeTemplateVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("goods")
public class GoodsController {

    @DubboReference
    private GoodsService goodsService;
    @DubboReference
    private CatService catService;
    @DubboReference
    private TypeTemplateService typeTemplateService;

    @RequestMapping("toAdd")
    public String toAdd(){
        return "admin/goods_edit";
    }

    @RequestMapping("addGoods")
    @ResponseBody
    public Result addGoods (TbGoods tbGoods, TbGoodsDesc tbGoodsDesc,String itemS){

        List<TbItem> tbItems = JSONUtil.toList(itemS, TbItem.class);

        TbSeller seller = (TbSeller) SecurityUtils.getSubject().getPrincipal();
        tbGoods.setSellerId(seller.getSellerId());
        tbGoods.setAuditStatus("0");
        tbGoods.setIsMarketable("1");

        tbItems.forEach(tbItem -> {
            tbItem.setSellerId(seller.getSellerId());
            tbItem.setSeller(seller.getName());
        });

        try {
            goodsService.addGoods(tbGoods,tbGoodsDesc,tbItems);
            return Result.OK();
        }catch (Exception e){
            return Result.ER();
        }
    }
    @RequestMapping("listCat")
    @ResponseBody
    public Result listCat(@RequestParam(defaultValue = "0")Long parentId){
        try {
            List<TbItemCat> list = catService.listCat(parentId);
            return Result.OK().setData(list);
        }catch (Exception e){
            return Result.ER();
        }
    }
    @RequestMapping("getTypeIdById")
    @ResponseBody
    public Result getTypeIdById(Long id){
        try {
            TbItemCat tbItemCat = catService.getTypeIdById(id);
            return Result.OK().setData(tbItemCat);
        }catch (Exception e){
            return Result.ER();
        }
    }
    @RequestMapping("getTypeById")
    @ResponseBody
    public Result getTypeById(Long id){
        try {
            TbTypeTemplate tbTypeTemplate = typeTemplateService.toUpdate(id);
            return Result.OK().setData(tbTypeTemplate);
        }catch (Exception e){
            return Result.ER();
        }
    }
    @RequestMapping("getTypeTemplateVoById")
    @ResponseBody
    public Result getTypeTemplateVoById(Long id){
        try {
            TypeTemplateVo typeTemplateVo = typeTemplateService.getTypeTemplateVoById(id);
            return Result.OK().setData(typeTemplateVo);
        }catch (Exception e){
            return Result.ER();
        }
    }
}
